package com.nari.ies.ne.nw.server.service.fileDataCheck.impl;

import com.nari.ies.ne.nw.server.entity.FileDataCheckEntity;
import com.nari.ies.ne.nw.server.mapper.FileDataCheckMapper;
import com.nari.ies.ne.nw.server.mapper.PlantDataMapper;
import com.nari.ies.ne.nw.server.service.fileDataCheck.ITJXXFileDataCheckService;
import com.nari.ies.ne.nw.server.util.DataUtil;
import com.nari.ies.ne.nw.server.util.DateUtil;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class TJXXFileDataCheckServiceImpl implements ITJXXFileDataCheckService {
    @Autowired
    private PlantDataMapper plantDataMapper;

    @Autowired
    private FileDataCheckMapper fileDataCheckMapper;

    @Override
    public void FDTJXXFileDataCheck(File file) {
        //获取当前时间最接近的整15分钟时间
        Date date = DateUtil.getLastQuarter();

//        System.out.println("数据评分--------------------" + file.getName());
        log.info("数据评分--------------------" + file.getName());
        FileDataCheckEntity fileDataCheckEntity;
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型
        int fansNumberNoData = 0;//风机台数无数据
        int fansNumberQualifiedData = 0;//风机台数合格数据(有数据且合格)
        int fansNumberUnqualifiedData = 0;//风机台数不合格数据(有数据但不合格)
        int capacityNoData = 0;//容量无数据
        int capacityQualifiedData = 0;//容量合格数据(有数据且合格)
        int capacityUnqualifiedData = 0;//容量不合格数据(有数据但不合格)
        int activePowerNoData = 0;//全站有功功率无数据
        int activePowerQualifiedData = 0;//全站有功功率合格数据(有数据且合格)
        int activePowerUnqualifiedData = 0;//全站有功功率不合格数据(有数据但不合格)
        int reactivePowerNoData = 0;//全站无功功率无数据
        int reactivePowerQualifiedData = 0;//全站无功功率合格数据(有数据且合格)
        int reactivePowerUnqualifiedData = 0;//全站无功功率不合格数据(有数据但不合格)
        int availablePowerNoData = 0;//全站可利用功率无数据
        int availablePowerQualifiedData = 0;//全站可利用功率合格数据(有数据且合格)
        int availablePowerUnqualifiedData = 0;//全站可利用功率不合格数据(有数据但不合格)

        //表名
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_TJXX";

        // 文件名称 FD_GD.GDZJFSFD_CDQYC_20230612_073000.dat
        String fileName = file.getName();

        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);
        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }
        //台账装机
        float windPlantCapacityData = plantDataMapper.selectPlantCapacityData(Long.parseLong(plantId));
        log.info("风电台账装机："+windPlantCapacityData);

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 读取到的每一行数据
            String line = "";

            while ((line = br.readLine()) != null) {
                // 预测时间标签行
                if (line.contains("<TJXX")) {
                    forecastDate = DataUtil.queryTime(line);
                }
                String[] strs = line.split("\t");

                // 数据行
                if (line.contains("#")) {

                    // 风机台数
                    if (!" ".equals(strs[2]) && !"null".equals(strs[2]) && !"NULL".equals(strs[2])) {
                        if (Float.parseFloat(strs[2]) >= 0) {
                            fansNumberQualifiedData++;
                        }
                    }

                    // 装机容量
                    if (!" ".equals(strs[3]) && !"null".equals(strs[3]) && !"NULL".equals(strs[3])) {
                        if (Float.parseFloat(strs[3]) >= 0) {
                            capacityQualifiedData++;
                        }
                    }

                    // 全站有功功率
                    if (!" ".equals(strs[4]) && !"null".equals(strs[4]) && !"NULL".equals(strs[4])) {
                    if (Float.parseFloat(strs[4]) >= -0.05*windPlantCapacityData && Float.parseFloat(strs[4]) <= windPlantCapacityData) {
                            activePowerQualifiedData++;
                        }
                    }

                    // 全站无功功率
                    if (!" ".equals(strs[5]) && !"null".equals(strs[5]) && !"NULL".equals(strs[5])) {
                        if (Float.parseFloat(strs[4]) >= 0) {
                            reactivePowerQualifiedData++;
                        }
                    }

                    // 全站可利用有功功率
                    if (!" ".equals(strs[6]) && !"null".equals(strs[6]) && !"NULL".equals(strs[6])) {
                        if (Float.parseFloat(strs[4]) >= 0) {
                            availablePowerQualifiedData++;
                        }
                    }

                }
            }

            // 风机台数
            dataType = "fansNumber";
            // 风机台数不合格
            fansNumberUnqualifiedData = 1 - fansNumberQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, fansNumberNoData, fansNumberQualifiedData, fansNumberUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 装机容量
            dataType = "capacity";
            // 装机容量不合格
            capacityUnqualifiedData = 1 - capacityQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, capacityNoData, capacityQualifiedData, capacityUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 全站有功功率
            dataType = "activePower";
            // 全站有功功率不合格
            activePowerUnqualifiedData = 1 - activePowerQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, activePowerNoData, activePowerQualifiedData, activePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 全站无功功率
            dataType = "reactivePower";
            // 全站无功功率不合格
            reactivePowerUnqualifiedData = 1 - reactivePowerQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, reactivePowerNoData, reactivePowerQualifiedData, reactivePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 全站可利用有功功率
            dataType = "availablePower";
            // 全站可利用有功功率不合格
            availablePowerUnqualifiedData = 1 - availablePowerQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, availablePowerNoData, availablePowerQualifiedData, availablePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);


            System.out.println(Arrays.toString(dataList.toArray()));

            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate, tableName);
            if (data == 0) {
                log.info("数据评分插入--------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList, tableName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }

    @Override
    public void GFTJXXFileDataCheck(File file) {
        log.info("数据评分--------------------"+file.getName());
        FileDataCheckEntity fileDataCheckEntity;
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型

        // 箱变台数
        int xbNumNoData = 0;//无数据
        int xbNumQualifiedData = 0;//有数据且合格
        int xbNumQualifiedNo = 0;//有数据但不合格

        // 逆变器台数
        int nbqNumNoData = 0;//无数据
        int nbqNumQualifiedData = 0;//有数据且合格
        int nbqNumQualifiedNo = 0;//有数据但不合格

        // 装机容量
        int capacityNoData = 0;//无数据
        int capacityQualifiedData = 0;//有数据且合格
        int capacityQualifiedNo = 0;//有数据但不合格

        // 全站有功功率
        int powerNodata = 0;//无数据
        int powerQualifiedData = 0;//有数据且合格
        int powerQualifiedNo = 0;//有数据但不合格

        // 全站无功功率
        int reactivePowerNodata = 0;//无数据
        int reactivePowerQualifiedData = 0;//有数据且合格
        int reactivePowerQualifiedNo = 0;//有数据但不合格

        // 全站可利用有功功率
        int availablePowerNodata = 0;//无数据
        int availablePowerQualifiedData = 0;//有数据且合格
        int availablePowerQualifiedNo = 0;//有数据但不合格

        String tableName = "HISDB.NW_NE_FILE_DATACHECK_GF_TJXX";//表名

        // 文件名称
        String fileName = file.getName();

        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);

        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }

        float pvPlantCapacityData = plantDataMapper.selectPlantCapacityData(Long.parseLong(plantId));
        log.info("光伏台账装机："+pvPlantCapacityData);

        FileReader fr = null;
        BufferedReader br = null;

        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 行数据
            String line = "";
            while ((line = br.readLine()) != null) {
                // 获取文件的创建时间
                if (line.contains("<TJXX")) {
                    forecastDate = DataUtil.queryTime(line);
                }
                String[] strs = line.split("\t");
                if (line.contains("#")) {
                    //箱变台数
                    if(!" ".equals(strs[2]) && !"null".equals(strs[2]) && !"NULL".equals(strs[2])){
                        if(Float.parseFloat(strs[2])>=0){
                            xbNumQualifiedData++;
                        }
                    }
                    //逆变器台数
                    if(!" ".equals(strs[3]) && !"null".equals(strs[3]) && !"NULL".equals(strs[3])){
                        if(Float.parseFloat(strs[3])>=0){
                            nbqNumQualifiedData++;
                        }
                    }
                    //装机容量
                    if(!" ".equals(strs[4]) && !"null".equals(strs[4]) && !"NULL".equals(strs[4])){
                        if(Float.parseFloat(strs[4])>=0){
                            capacityQualifiedData++;
                        }
                    }
                    // 全站有功功率
                    if(!" ".equals(strs[5]) && !"null".equals(strs[5]) && !"NULL".equals(strs[5])){
                        if (Float.parseFloat(strs[4]) >= -0.05*pvPlantCapacityData && Float.parseFloat(strs[4]) <= pvPlantCapacityData) {
                            powerQualifiedData++;
                        }
                    }
                    // 全站无功功率
                    if(!" ".equals(strs[6]) && !"null".equals(strs[6]) && !"NULL".equals(strs[6])){
                        reactivePowerQualifiedData++;
                    }
                    // 全站可利用有功功率
                    if(!" ".equals(strs[7]) && !"null".equals(strs[7]) && !"NULL".equals(strs[7])){
                        availablePowerQualifiedData++;
                    }
                }
            }

            // 箱变台数
            dataType="xbNum";
            xbNumQualifiedNo=1-xbNumQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, xbNumNoData, xbNumQualifiedData, xbNumQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 逆变器台数
            dataType="nbqNum";
            nbqNumQualifiedNo=1-nbqNumQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, nbqNumNoData, nbqNumQualifiedData, nbqNumQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 装机容量
            dataType="capacity";
            capacityQualifiedNo=1-capacityQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, capacityNoData, capacityQualifiedData, capacityQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 全站有功功率
            dataType="power";
            powerQualifiedNo=1-powerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, powerNodata, powerQualifiedData, powerQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 全站无功功率
            dataType="reactivePower";
            reactivePowerQualifiedNo=1-reactivePowerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, reactivePowerNodata, reactivePowerQualifiedData, reactivePowerQualifiedNo);
            dataList.add(fileDataCheckEntity);


            // 全站可利用有功功率
            dataType="availablePower";
            availablePowerQualifiedNo=1-availablePowerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, availablePowerNodata, availablePowerQualifiedData, availablePowerQualifiedNo);
            dataList.add(fileDataCheckEntity);

            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate, tableName);
            if (data == 0) {
                log.info("数据评分插入--------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList, tableName);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }

}
